package com.ruoyi.framework.mybatis;

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;


@Configuration
// 这一行很重要，因为interceptor 链的执行与添加是反序的，所以在 PageHelperAutoConfiguration 之后添加，才能先执行。
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisInterceptorAutoConfiguration {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Autowired
    private AutoInjectBaseFieldInterceptor autoInjectBaseFieldInterceptor;

    @Autowired
    private DataScopeInterceptors dataScopeInterceptors;
    @PostConstruct
    public void addMyInterceptor() {
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
            configuration.addInterceptor(autoInjectBaseFieldInterceptor);
            configuration.addInterceptor(dataScopeInterceptors);
        }
    }
}
